//
//  MeViewController.m
//  gosh
//
//  Created by 许小军 on 16/9/22.
//  Copyright © 2016年 wyzc. All rights reserved.
//

#import "MeViewController.h"
#import "XXJCollectionViewCell.h"
#import <MJExtension/MJExtension.h>
#import <AFNetworking/AFNetworking.h>
#import <UIImageView+WebCache.h>
#import "XXJCollectionModel.h"
#import "XXJWebViewController.h"
static NSString * const ID=@"cell";

@interface MeViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
//{
//    NSMutableArray * _itemsArr;
//}
@property(weak,nonatomic)UICollectionView * collectionView;
@property(strong,nonatomic)NSMutableArray * itemsArr;
@end

@implementation MeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor lightGrayColor];
    
    [self setUpNavBtn];
    
    
    [self setUpFootView];
    
    [self loadData];
    self.tableView.sectionHeaderHeight=0;
    self.tableView.sectionFooterHeight=10;
    self.tableView.contentInset=UIEdgeInsetsMake(-25, 0, 0, 0);
}

-(void)loadData
{
    AFHTTPSessionManager * manager=[AFHTTPSessionManager manager];
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"a"] = @"square";
    parameters[@"c"] = @"topic";
    
    [manager GET:@"http://api.budejie.com/api/api_open.php" parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        //[responseObject writeToFile:@"/Users/duoduo/Desktop/xxxxxxxxx.plist" atomically:YES];
        NSArray * modelArray=responseObject[@"square_list"];
       _itemsArr= [XXJCollectionModel mj_objectArrayWithKeyValuesArray:modelArray];
        
        [self resloveData];//处理缺少的内容展示空的
        NSInteger hang= (_itemsArr.count-1)/4+1;
        self.collectionView.cur_h=(self.view.cur_w-3)/4*hang;
        self.tableView.tableFooterView=self.collectionView;//计算集合视图的高度等于脚的高度
        [self.collectionView reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error%@",error);
    }];
}

-(void)resloveData
{
    NSInteger other= _itemsArr.count%4;
    if (other) {
        NSInteger next=4-other;
        for (int i=0; i<next; i++) {
            XXJCollectionModel * model=[[XXJCollectionModel alloc]init];
            [_itemsArr addObject:model];
        }
    }
}
-(void)setUpFootView
{
    /*
     1、初始化要设置流水布局
     2、cell必须要注册
     3、cell必须要自定义
     */
    
    //创建布局
    UICollectionViewFlowLayout * layout=[[UICollectionViewFlowLayout alloc]init];
    NSInteger cols=4;
    CGFloat magin=1;
    CGFloat itrmWH=(self.view.cur_w-(cols-1)*magin)/cols;
    layout.itemSize=CGSizeMake(itrmWH, itrmWH);
    layout.minimumInteritemSpacing=magin;
    layout.minimumLineSpacing=magin;
    
    
    
    UICollectionView * collectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 0, 300) collectionViewLayout:layout];
    self.collectionView=collectionView;
    collectionView.scrollEnabled=NO;
    collectionView.backgroundColor=self.tableView.backgroundColor;
    self.tableView.tableFooterView=collectionView;
    
    
    collectionView.dataSource=self;
    collectionView.delegate=self;
    
    [collectionView registerNib:[UINib nibWithNibName:@"XXJCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:ID];
    
    //[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:ID];
}



#pragma mark--UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.itemsArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
   XXJCollectionViewCell * cell= [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    //cell.backgroundColor=[UIColor greenColor];
    XXJCollectionModel * model=self.itemsArr[indexPath.row];
    cell.model=model;
    
    return cell;
}

#pragma  maek --UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    XXJCollectionModel * model=_itemsArr[indexPath.row];
    XXJWebViewController * web=[[XXJWebViewController alloc]init];
    web.url=[NSURL URLWithString:model.url];
    web.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:web animated:YES];
}


-(void)setUpNavBtn
{
    UIButton * setBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [setBtn setImage:[UIImage imageNamed:@"mine-setting-icon"] forState:UIControlStateNormal];
    [setBtn setImage:[UIImage imageNamed:@"mine-setting-icon-click"] forState:UIControlStateHighlighted];
    [setBtn sizeToFit];
    //[setBtn addTarget:setBtn action:@selector(setting) forControlEvents:UIControlEventTouchUpInside];
    UIView * view1=[[UIView alloc]initWithFrame:setBtn.bounds];
    [view1 addSubview:setBtn];
    UIBarButtonItem * set=[[UIBarButtonItem alloc]initWithCustomView:view1];
    
    
    
    UIButton * nightBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [nightBtn setImage:[UIImage imageNamed:@"mine-moon-icon"] forState:UIControlStateNormal];
    [nightBtn setImage:[UIImage imageNamed:@"mine-moon-icon-click"] forState:UIControlStateSelected];
    [nightBtn sizeToFit];
    //[nightBtn addTarget:setBtn action:@selector(night) forControlEvents:UIControlEventTouchUpInside];
    UIView * view2=[[UIView alloc]initWithFrame:nightBtn.bounds];
    [view2 addSubview:nightBtn];
    UIBarButtonItem * nights=[[UIBarButtonItem alloc]initWithCustomView:view2];
    
    self.navigationItem.rightBarButtonItems=@[set,nights];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
